package com.rowline.admin.module.study.dynamicproxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * ClassName:DynamicProxy
 * Created by 245167
 * on 2017/12/5  05:35
 * description:
 */
public class DynamicProxy implements InvocationHandler{

    private Object subject;

    public DynamicProxy(Object subject) {
        this.subject = subject;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        System.out.println("before invoke");

        System.out.println("Method:" + method);

        method.invoke(subject,args);

        System.out.println("after invoke");

        return null;
    }

    public static void main(String[] args) {
        Subject realSubject = new RealSubject();
        InvocationHandler handler = new DynamicProxy(realSubject);
        Subject subject = (Subject) Proxy.newProxyInstance(handler.getClass().getClassLoader(),
                realSubject.getClass().getInterfaces(),
                handler);
        System.out.println(subject.getClass().getName());
        System.out.println("============================");
        subject.rent();
        System.out.println("============================");
        subject.hello("hello");
        System.out.println("============================");
    }
}
